package com.health.mapp.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.health.db.model.PatientList;
import com.health.db.model.UserLIST;
import com.health.db.service.user.IPatientListService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.AddPatientReq;
import com.health.mapp.bean.AddPatientResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.Response;

public class AddPatientImpl implements ActionInterface {

	private IUserLISTService userLISTService ;
	private IPatientListService patientListService  ;

	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		// TODO Auto-generated method stub
		AddPatientReq req = (AddPatientReq) reqBean;
		AddPatientResp _resp = new AddPatientResp();
		Long userId = req.getUserId();
		String patName = req.getPatName();
		Short patSexId = req.getPatSex();
		String patBirthday = req.getPatBirthday();
		if (null == patName || patName.length() < 2) {
			_resp.setErrorCode(-1);
			_resp.setErrorMsg("就诊人姓名不能小于两位");
		} else {
			UserLIST userLIST = userLISTService.selectByUserId(userId);
			if (null == userLIST) {
				_resp.setErrorCode(-1);
				_resp.setErrorMsg("新增就诊人失败 该用户不存在");
			} else {
				PatientList patientList = new PatientList();
				patientList.setUserid(userId);
				patientList.setPatientName(req.getPatName());
				patientList.setSexid(patSexId);
				String patSex = patSexId == 0 ? "女" : "男";
				patientList.setSex(patSex);
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				patientList.setBirthdayDate(dateFormat.parse(patBirthday));
				patientList.setCreateDate(new Date());
				patientList.setUpdateDate(new Date());
				patientList.setRegisterDate(new Date());
				int result = patientListService.addPatientList(patientList);
				if (result > 0) {
					_resp.setPatId(String.valueOf(patientList.getPatientid()));
					_resp.setErrorCode(0);
					_resp.setErrorMsg("Success");
				} else {
					_resp.setErrorCode(-1);
					_resp.setErrorMsg("新增就诊人失败");
				}
			}
		}
		return _resp;
	}

	 

	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}



	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}



	public IPatientListService getPatientListService() {
		return patientListService;
	}

	public void setPatientListService(IPatientListService patientListService) {
		this.patientListService = patientListService;
	}

}
